import { isBoom, Boom } from '@hapi/boom';

import koa from 'koa';

export default async function (ctx: koa.Context, next: koa.Next) {
  try {
    await next();
  } catch (err) {
    if (isBoom(err)) {
      const berr = err as Boom;
      ctx.status = berr.output.statusCode;
      ctx.body = { msg: err.message };
    } else {
      ctx.status = 500;
      ctx.body = { msg: err.message };
      console.log(err.message + err.stack);
    }
  }
}
